package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class AddressController {
    @Autowired
    private AddressService addressService;

    //提交购物车，显示默认地址
    @GetMapping("/address/default")
    public ResultInfo addressDefault(){
        Address address = addressService.addressDefault();
        return ResultInfo.success(address);
    }

    //添加配送地址
    @PostMapping("/address")
    public ResultInfo addressAdd(@RequestBody Address address){
        addressService.addressAdd(address);
        return ResultInfo.success(null);
    }
    //显示全部地址
    @GetMapping("/address/list")
    public ResultInfo addressList(){
        List<Address> addressList = addressService.addressList();
        return ResultInfo.success(addressList);
    }
    //设为默认地址
    @PutMapping("/address/default")
    public ResultInfo updateAddressDefault(@RequestBody Address address){
        addressService.update(address);
        return ResultInfo.success(null);
    }
    //修改地址时复显地址
    @GetMapping("/address/{id}")
    public ResultInfo findAddressById(@PathVariable Long id){
        Address address = addressService.findById(id);
        return ResultInfo.success(address);
    }
    //修改地址
    @PutMapping("/address")
    public ResultInfo updateAddress(@RequestBody Address address){
        addressService.updateAddress(address);
        return ResultInfo.success(null);
    }
    //删除地址
    @DeleteMapping("/address")
    public ResultInfo deleteAddress(Long ids){
        addressService.deleteAddress(ids);
        return ResultInfo.success(null);
    }
}
